#include "ObjectHelper.h"
#include "Transforms.h"
#include "Color.h"


class Triangle {
    vec3 p1;
    vec3 p2;
    vec3 p3;
    vector<Transform> transforms;
    vector<Finish> finishes;
    Color color;
    
    public:
        Triangle() {
            p1 = vec3();
            p2 = vec3();
            p3 = vec3();
            color = Color();
        }
        
        Triangle(vector<string> input) {
            int index = 10;
            assert(input[0] == "triangle");
            
            istringstream(input[1]) >> p1.x;
            istringstream(input[2]) >> p1.y;
            istringstream(input[3]) >> p1.z;
            
            istringstream(input[4]) >> p2.x;
            istringstream(input[5]) >> p2.y;
            istringstream(input[6]) >> p2.z;
            
            istringstream(input[7]) >> p3.x;
            istringstream(input[8]) >> p3.y;
            istringstream(input[9]) >> p3.z;
            
            while (index < input.size()) {
                if (input[index] == "pigment") {
                    if (input[index + 1] == "color") {
                        if (input[index + 2] == "rgb") {
                            istringstream(input[index + 3]) >> color.r;
                            istringstream(input[index + 4]) >> color.g;
                            istringstream(input[index + 5]) >> color.b;
                        }
                    }
                    else {
                        cout << "Bad keyword found in pigment Sphere: " << input[index + 1] << endl;
                        assert(0);
                    }
                    
                    index += 6;
                }
                else if (input[index] == "finish") {
                    index++;
                    while (index < input.size()) {
                        if (input[index] == "ambient") {
                            float value = 0.0f;
                            
                            istringstream(input[index + 1]) >> value;
                            finishes.push_back(Finish(Ambient, value));
                            
                            index += 2;                         
                        }
                        else if (input[index] == "diffuse") {
                            float value = 0.0f;
                            
                            istringstream(input[index + 1]) >> value;
                            finishes.push_back(Finish(Diffuse, value));
                            
                            index += 2; 
                        }
                        else {
                            break;
                        }
                    }
                }
                else if (input[index] == "translate") {
                    vec3 offset;
                    
                    istringstream(input[index + 1]) >> offset.x;
                    istringstream(input[index + 2]) >> offset.y;
                    istringstream(input[index + 3]) >> offset.z;
                    
                    transforms.push_back(Translate(offset));
                    
                    index += 4;
                }
                else if (input[index] == "scale") {
                    vec3 ratio;
                    
                    istringstream(input[index + 1]) >> ratio.x;
                    istringstream(input[index + 2]) >> ratio.y;
                    istringstream(input[index + 3]) >> ratio.z;
                    
                    transforms.push_back(Scale(ratio));
                    
                    index += 4;
                }
                else if (input[index] == "rotate") {
                    vec3 axis;
                    
                    istringstream(input[index + 1]) >> axis.x;
                    istringstream(input[index + 2]) >> axis.y;
                    istringstream(input[index + 3]) >> axis.z;
                    
                    transforms.push_back(Rotate(axis));
                    
                    index += 4;
                }
                else {
                    cout << "Bad keyword found in Triangle: " << input[index] << endl;
                    assert(0);
                }    
            }
        }
};
